package top.kuxiao.crmserver.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.kuxiao.crmserver.mapper.*;
import top.kuxiao.crmserver.pojo.Opportunity;
import top.kuxiao.crmserver.pojo.SaleChance;
import top.kuxiao.crmserver.service.ISaleChanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 销售机会 服务实现类
 * </p>
 *
 * @author kuxiao
 * @since 2022-01-12
 */
@Service
public class SaleChanceServiceImpl extends ServiceImpl<SaleChanceMapper, SaleChance> implements ISaleChanceService {

    /**
     * 销售机会mapper
     */
    @Autowired
    SaleChanceMapper saleChanceMapper;
    /**
     * 客户经理mapper
     */
    @Autowired
    AccountManagerMapper accountManagerMapper;
    /**
     * 项目主管mapper
     */
    @Autowired
    SalesExecutiveMapper salesExecutiveMapper;
    /**
     * 销售机会来源mapper
     */
    @Autowired
    OpportunityMapper opportunityMapper;
    /**
     * 销售机会状态mapper
     */
    @Autowired
    OpportunityStateMapper opportunityStateMapper;

    @Override
    public List<SaleChance> queryAll() {
        List<SaleChance> list = saleChanceMapper.selectList(null);  // 获取所有销售机会
        for (SaleChance saleChance : list) {  // 遍历销售机会
            Integer oppId = saleChance.getOppId();  // 取出销售机会来源id
            Opportunity opportunity = opportunityMapper.selectById(oppId);  // 通过id查询机会来源
            saleChance.setOpportunity(opportunity);  // 设置机会来源
            // 机会来源状态
            saleChance.setOpportunityState(opportunityStateMapper.selectById(saleChance.getOppStateId()));
            // 客户经理 (或许为空)
            if (saleChance.getScAssignToId() != null){
                saleChance.setScAssignTo(accountManagerMapper.selectById(saleChance.getScAssignToId()));
            }
        }
        return list;
    }
}
